package org.sbbs.app.license.model;

import java.io.UnsupportedEncodingException;
import java.util.List;

public class License {
    public String getCustomer() {
        return customer;
    }

    public void setCustomer( String customer ) {
        this.customer = customer;
    }

    public String getVersionNumber() {
        return versionNumber;
    }

    public void setVersionNumber( String versionNumber ) {
        this.versionNumber = versionNumber;
    }

    public String getCopyrightInfo() {
        return copyrightInfo;
    }

    public void setCopyrightInfo( String copyrightInfo ) {
        this.copyrightInfo = copyrightInfo;
    }

    public List<Product> getProducts() {
        return products;
    }

    public void setProducts( List<Product> products ) {
        this.products = products;
    }

    private String customer;// 40

    private String versionNumber;// 10

    private String copyrightInfo;// 50

    private List<Product> products;

    /**
     * @return
     */
    public byte[] getRoMemoryInfo() {
        try {
            byte[] roMem = new byte[100];
            byte[] cusbyte = this.customer.getBytes( "GBK" );
            byte[] verbyte = this.versionNumber.getBytes( "GBK" );
            byte[] copybyte = this.copyrightInfo.getBytes( "GBK" );
            roMem = this.mergeByteArray( cusbyte, roMem, 0, cusbyte.length );
            roMem = this.mergeByteArray( verbyte, roMem, 40, verbyte.length );
            roMem = this.mergeByteArray( copybyte, roMem, 50, copybyte.length );
            return roMem;
        }
        catch ( UnsupportedEncodingException e ) {
            e.printStackTrace();
            return null;
        }

    }

    public byte[] getRwMemoryInfo() {
        byte[] rwMem = new byte[products.size() * 12];
        try {
            for ( int i = 0; i < products.size(); i++ ) {
                Product pd = (Product) products.get( i );
                byte[] pid = pd.getProductId().getBytes( "GBK" );
                byte[] un = pd.getAllowedUsers().getBytes( "GBK" );
                
                byte[] prob = new byte[12];
                prob = this.mergeByteArray( pid, prob, 0, pid.length );
                prob = this.mergeByteArray( un, prob, 8, un.length );
                
                this.mergeByteArray( prob, rwMem, 12*i, 12 );
                
                

            }
            return rwMem;

        }
        catch ( UnsupportedEncodingException e ) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }

    }

    private byte[] mergeByteArray( byte[] src, byte[] tar, int offset, int length ) {

        System.arraycopy( src, 0, tar, offset, length );

        return tar;
    }
}
